package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.StrategyMainMapper;
import com.kaili.basic.pojo.dto.StrategyMainQueryDto;
import com.kaili.basic.pojo.entity.StrategyMainEntity;
import com.kaili.basic.pojo.vo.StrategyMainVO;
import com.kaili.basic.service.*;
import com.kaili.common.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
* 策略管理主 service实现类
* @author:zhangjl
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class StrategyMainServiceImpl extends ServiceImpl<StrategyMainMapper, StrategyMainEntity>  implements IStrategyMainService {

    @Autowired
    IStrategySiteService strategySiteService;
    @Autowired
    IStrategyWifiService strategyWifiService;
    @Autowired
    IGlobalClobService globalClobService;
    @Autowired
    IStrategyUpgradeService strategyUpgradeService;


    @Override
    public StrategyMainVO queryById(Integer id) {
        List<StrategyMainVO> list = baseMapper.queryList(new HashMap<String, Object>(2) {{put("id", id);}});
        if(list !=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<StrategyMainVO> queryList(StrategyMainQueryDto dto) {
        return baseMapper.queryList(ObjectUtils.objectToMap(dto));
    }

    @Override
    public Page<StrategyMainVO> queryPageList(Page<StrategyMainVO> page, StrategyMainQueryDto dto) {
        return page.setRecords(baseMapper.queryPageList(page, ObjectUtils.objectToMap(dto)));
    }

    @Override
    public List<StrategyMainVO> queryExportList(StrategyMainQueryDto dto) {
        return baseMapper.queryPageList(ObjectUtils.objectToMap(dto));
    }

    @Override
    public boolean add(StrategyMainEntity entity) {

        //1、策略主表
        baseMapper.insert(entity);
        //2、设备策略表
        strategySiteService.add(entity);
        //3、WIFI配置表
        strategyWifiService.add(entity);
        //4、复选数据表
        globalClobService.add(entity);

        return true;
    }

    @Override
    public boolean addApply(StrategyMainEntity entity) {

        //1、策略主表
        baseMapper.insert(entity);
        //2、复选数据表
        globalClobService.add(entity);

        return true;
    }

    @Override
    public boolean addUpgrade(StrategyMainEntity entity) {

        //1、策略主表
        baseMapper.insert(entity);
        //2、升级类策略表
        strategyUpgradeService.add(entity);

        return true;
    }

    @Override
    public boolean modify(StrategyMainEntity entity) {
        //1、策略主表
        baseMapper.updateById(entity);
        //2、设备类策略表
        strategySiteService.modify(entity);
        //3、WIFI配置表
        strategyWifiService.modify(entity);
        //4、复选数据表
        globalClobService.modify(entity);

        return true;
    }

    @Override
    public boolean modifyApply(StrategyMainEntity entity) {
        //1、策略主表
        baseMapper.updateById(entity);
        //2、复选数据表
        globalClobService.modify(entity);

        return true;
    }

    @Override
    public boolean modifyUpgrade(StrategyMainEntity entity) {
        //1、策略主表
        baseMapper.updateById(entity);
        //2、升级类策略表
        strategyUpgradeService.modify(entity);

        return true;
    }

    @Override
    public Boolean updateStatus(List<Integer> idList, Integer status) {
        if (baseMapper.updateStatus(idList, status) > 0){
            return true;
        }

        return false;
    }

}
